package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.Pyq;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.ObjectUtil;
import com.qianniu.napi.common.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-12-17
 */
@RestController
public class PrintController {

    private static final Logger logger = LoggerFactory.getLogger(PrintController.class);

    @Autowired
    private IRedisService redisService;

    /*
     * 首页派单接口列表
     * 派单类别：type
     */
    @RequestMapping("/print/callback")
    @ResponseBody
    public Pdo callback(@RequestParam(value = "mcode", required = false) String mcode,
                        @RequestParam(value = "code", required = false) String code,
                        HttpServletRequest req) {
        logger.debug("-----------------------------------print/callback");
//        https://niucaiduo.com/print/callback?code=fc5d65e529664313b9a46ec60c9b99ec&state=1&response_type=code

        if (Strings.isNullOrEmpty(mcode)) mcode = "4004668858";
        String mckey = Constants.REDIS_PRINTER_CODE + mcode;
        String cckey = Constants.REDIS_PRINTER_CCODE + code;

        redisService.set(mckey, code);
        redisService.set(cckey, mcode);

        Pdo pdo = new Pdo();
        pdo.setData("OK");
        return pdo;
    }

    /*
     * 首页派单接口列表
     * 派单类别：type
     */
    @RequestMapping("/print/okstatus")
    @ResponseBody
    public Pdo okstatus(@RequestParam(value = "pid", required = false) Integer pid,
                        HttpServletRequest req) {
        logger.debug("-----------------------------------print/okstatus");

        Pdo pdo = new Pdo();
        pdo.setData("OK");
        return pdo;
    }

    /*
     * 接单拒绝推送地址
     */
    @RequestMapping("/print/refuse")
    @ResponseBody
    public Pdo prefuse(@RequestParam(value = "pid", required = false) Integer pid, HttpServletRequest req) {
        logger.debug("-----------------------------------print/refuse");

        Pdo pdo = new Pdo();
        pdo.setData("OK");
        return pdo;
    }

    /*
     * 终端状态推送地址
     */
    @RequestMapping("/print/terminal")
    @ResponseBody
    public Pdo terminal(@RequestParam(value = "pid", required = false) Integer pid,
                        @RequestParam(value = "status", required = false) Integer status,
                        HttpServletRequest req) {
        logger.debug("-----------------------------------print/terminal");

        Pdo pdo = new Pdo();
        pdo.setData("OK");
        return pdo;
    }

    /*
     * 案件请求推送地址
     */
    @RequestMapping("/print/tabkey")
    @ResponseBody
    public Pdo tabkey(@RequestParam(value = "pid", required = false) Integer pid,
                      HttpServletRequest req) {
        logger.debug("-----------------------------------print/tabkey");

        Pdo pdo = new Pdo();
        pdo.setData("OK");
        return pdo;
    }


    /*
     * 首页派单接口列表
     * 派单类别：type
     * 经度：lng
     * 纬度：lat
     * 第几页：pageIndex
     * 每页大小：pageSize
     */
    @RequestMapping("/pyq/hylist")
    @ResponseBody
    public Jinfo hylist(@RequestParam(value = "type", required = false) Integer type,
                        @RequestParam(value = "lng", required = false) Double lng,
                        @RequestParam(value = "lat", required = false) Double lat,
                        @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                        @RequestParam(value = "pageSize", required = false) Integer pageSize,
                        HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;

//        List<Pyq> list = pyqService.getHyList(pageIndex, pageSize, lat, lng, ObjectUtil.getMpid(), type, null);
//        info.setInfo(list);

        return info;
    }


    private class Pdo {

        private int code = 0;

        private String msg = null;

        private Object data;
        private Object extra;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public Object getExtra() {
            return extra;
        }

        public void setExtra(Object extra) {
            this.extra = extra;
        }
    }

}
